package com.core.ui.customtag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.ITableViewDatasource;
import com.core.protocols.ITableViewDelegate;
import com.core.ui.customcell.TableViewCell;
import com.core.ui.customview.TableViewHeaderCell;
import com.core.utils.IndexPath;

public class SimpleGrid
        extends TagSupport
        implements ICustomDatasourceDelegate {



	private void drawBody ( final StringBuilder stringBuilder ) {

		final int numberOfRows = datasource.numberOfDataRows();
		final int numberOfCols = datasource.numberOfDataCols();
		// draw table body
		for ( int i = 0; i < numberOfRows; i++ ) {
			String rowStyleClass = "";
			if ( delegate != null ) {
				rowStyleClass = delegate.rowStyleClassNameAtIndex(i);
			}
			final String rowFormat = (rowStyleClass == null) ? "<tr>" : String.format("<tr class=\"%s\">", rowStyleClass);
			stringBuilder.append(rowFormat);

			for ( int j = 0; j < numberOfCols; j++ ) {
				final TableViewCell cell = datasource.dataCellAtIndexPath(IndexPath.pathWithRowAndCol(i, j));
				if ( cell == null ) {
					continue;
				}

				stringBuilder.append(cell.toString());
			}

			stringBuilder.append("</tr>");
		}

	}



	private void drawHeader ( final StringBuilder stringBuilder ) {

		final int headerRowsCount = datasource.numberOfHeadRows();
		final int headerColsCount = datasource.numberOfHeadCols();

		// draw table header
		for ( int i = 0; i < headerRowsCount; i++ ) {

			String headerRowStyleClass = "";
			if ( delegate != null ) {
				headerRowStyleClass = delegate.headerRowStyleClassNameAtIndex(i);
			}

			final String headerRowFormat = (headerRowStyleClass == null) ? "<tr>" : String.format("<tr class=\"%s\">", headerRowStyleClass);
			stringBuilder.append(headerRowFormat);
			for ( int j = 0; j < headerColsCount; j++ ) {
				final TableViewHeaderCell headerCell = datasource.headCellAtIndexPath(IndexPath.pathWithRowAndCol(i, j));
				if ( headerCell == null ) {
					continue;
				}

				stringBuilder.append(headerCell.toString());
			}
			stringBuilder.append("</tr>");
		}
	}



	@Override
	public void refreshContent () {

		try {
			final JspWriter writer = pageContext.getOut();


			final StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("<div>");
			if ( (styleClassName == null) || (styleClassName.isEmpty()) ) {
				stringBuilder.append("<table border=\"1\" style=\"display:table;\">");
			} else {
				final String style = String.format("class=\"%s\"", styleClassName);
				stringBuilder.append(String.format("<table %s\">", style));

			}


			this.drawHeader(stringBuilder);

			this.drawBody(stringBuilder);

			stringBuilder.append("</table>");
			stringBuilder.append("</div>");
			writer.write(stringBuilder.toString());
		} catch ( IOException e ) {

		}
	}

	private static final long serialVersionUID = 4309654055444130457L;
	private String            styleClassName;



	public String getStyleClassName () {

		return styleClassName;
	}



	public void setStyleClassName ( final String styleClassName ) {

		this.styleClassName = styleClassName;
	}

	private String name;



	public String getName () {

		return name;
	}



	public void setName ( final String name ) {

		this.name = name;
	}

	private ITableViewDelegate delegate;



	public ITableViewDelegate getDelegate () {

		return delegate;
	}



	public void setDelegate ( final ITableViewDelegate delegate ) {

		this.delegate = delegate;
	}

	private ITableViewDatasource datasource;



	public ITableViewDatasource getDatasource () {

		return datasource;
	}



	public void setDatasource ( final ITableViewDatasource datasource ) {

		this.datasource = datasource;
		if ( this.datasource != null ) {
			this.datasource.setCustomDatasourceDelegate(this);
		}
	}



	public int doStartTag ()
	        throws JspException {

		if ( datasource != null ) {
			datasource.reloadData();
		}

		return SKIP_BODY;
	}

}
